//
// Created by xili on 2024/7/31 18:26.
// Go big or go home.
//
#include <iostream>
#include <algorithm>

using namespace std;

//转化成字符串翻转
class Solution1 {
public:
    int reverse(int x) {
        bool isNegative = x < 0;
        int n = abs(x);
        string s = to_string(n);
        ::reverse(s.begin(), s.end());
        // 如果字符串只有一个字符并且是 '0' 则不需要去除
        if (s[0] == '0' && s.length() > 1) {
            s.erase(0, s.find_first_not_of('0'));
        }
        long ans = stol(s);
        if (isNegative) {
            ans = -ans;
        }
        return (ans > INT32_MAX || ans < INT32_MIN) ? 0 : static_cast<int>(ans);
    }
};

//
class Solution2 {
public:
    int reverse(int x) {
        long ans = 0;
        while (x != 0) {
            ans = ans * 10 + x % 10;
            x /= 10;
        }
        return (ans > INT32_MAX || ans < INT32_MIN) ? 0 : ans;
    }
};

